Page({
  data: {
    category: '',
    activeFilter: 'default',
    serviceList: [],
    page: 1,
    pageSize: 10,
    noMoreData: false
  },

  onLoad: function(options) {
    if (options.category) {
      this.setData({
        category: options.category
      });
    }
    this.loadServiceList();
  },

  loadServiceList: function() {
    const app = getApp();
    const { category, page, pageSize } = this.data;
    
    app.request({
      url: `${app.globalData.baseUrl}/services/`,
      data: {
        category: category,
        page: page,
        page_size: pageSize
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const newList = res.data.results || res.data;
          const serviceList = page === 1 ? newList : [...this.data.serviceList, ...newList];
          
          this.setData({
            serviceList: serviceList,
            noMoreData: newList.length < pageSize
          });
        }
      }
    });
  },

  filterByDefault: function() {
    this.setData({
      activeFilter: 'default',
      page: 1
    });
    this.loadServiceList();
  },

  filterByPrice: function() {
    this.setData({
      activeFilter: 'price',
      page: 1
    });
    this.loadServiceList();
  },

  filterByRating: function() {
    this.setData({
      activeFilter: 'rating',
      page: 1
    });
    this.loadServiceList();
  },

  goToServiceDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/customer/serviceDetail?id=${id}`
    });
  },

  onReachBottom: function() {
    // 上拉加载更多
    if (!this.data.noMoreData) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadServiceList();
    }
  }
})